package com.mdc.mansion.api.web.modules.system.web.resources;

import com.google.common.collect.Maps;
import com.mdc.mansion.api.web.common.web.BaseController;
import com.mdc.mansion.api.web.modules.system.utils.UploadUtils;
import com.mdc.mansion.common.exception.BusinessException;
import com.mdc.mansion.common.response.Result;
import com.mdc.mansion.common.response.ResultCode;
import com.mdc.mansion.common.util.I18nUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.Map;

@Controller
@RequestMapping(value = "${adminPath}/resource")
public class ResourcesController extends BaseController{

    @RequestMapping("upload")
    public Result upload(MultipartHttpServletRequest request){
        Result result=new Result(ResultCode.COMMON_SUCCESS);
        Map<String, String> data= Maps.newHashMap();
        MultipartFile uploadFile = request.getFile("file");
        if(uploadFile==null){throw new BusinessException(I18nUtil.getMessage("param.error"));}
        try {
            String[] uploadFile2 = UploadUtils.uploadFile(uploadFile,"");
            data.put("filePath", uploadFile2[1]);
            data.put("fileUrl", uploadFile2[2]);
            result.setProperty("fileUrl",data);
        } catch (Exception e) {
            e.printStackTrace();
            this.loggerError(e, "上传文件出错");
            throw new BusinessException(I18nUtil.getMessage("param.error"));
        }
        return result;
    }
}
